package com.qiyang.log.controller;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.qiyang.api.log.LogApi;
//import LogApiRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created on 2018/4/29.
 *
 * @author sehuaqi.
 * 描述：
 */
//@RestController
@RefreshScope
@HSFProvider(serviceInterface = LogApi.class, serviceVersion = "1.0.0")
public class LogController implements LogApi {

    Logger logger = LoggerFactory.getLogger(LogController.class);

    @Autowired
    private RestTemplate template;

    @Value("${server.port}")
    private String serverPort;

    @Value("${dev.name}")
    private String infoName;

    /**
     * 打印日志信息
     *
     * @param info
     * @return
     */
    @Override
//    @RequestMapping("/printLog")
//    @HystrixCommand(fallbackMethod = "printLogError")
    public String printLog(String info) {

        logger.info("<" + info + ">");

        //String baiduHtml = template.getForObject("https://docs.teambition.com/all/doc", String.class);

        //logger.info(baiduHtml.substring(50));

        String serverHost = null;

        try {
            serverHost = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return "succ:from-->" + serverHost + ":" + serverPort + " -- " + infoName;
    }

    public String printLogError(String info) {

        logger.info("< error:" + info + ">");

        return "error";
    }


}
